package ihm;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import metier.Tarif;
import dao.DaoProjetCinema;

public class FenetreModifSupTarif extends JInternalFrame
{
	private JLabel lblLibelle;
	private JComboBox <String>lstTarif;
	private JPanel panel;
	private JTextField txtLibelle;
	private JLabel lblNvLibelle;
	private JLabel lblNvPrix;
	private JTextField txtPrix;
	private JPanel panel_1;
	private JButton btnSupprimer;
	private JButton btnModifier;
	private JLabel lblId;
	private JTextField txtId;
	private Vector<Tarif> lesTarifs;

	/**
	 * Create the frame.
	 */
	public FenetreModifSupTarif(Vector<Tarif> lesTarifs)
	{
		setBounds(100, 100, 785, 464);
		getContentPane().setLayout(null);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "modification", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel.setBounds(232, 154, 328, 145);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		txtLibelle = new JTextField();
		txtLibelle.setBounds(95, 50, 173, 20);
		panel.add(txtLibelle);
		txtLibelle.setColumns(10);
		
		lblNvLibelle = new JLabel("Libell\u00E9 :");
		lblNvLibelle.setBounds(22, 50, 46, 14);
		panel.add(lblNvLibelle);
		
		lblNvPrix = new JLabel("Prix :");
		lblNvPrix.setBounds(22, 90, 46, 14);
		panel.add(lblNvPrix);
		
		txtPrix = new JTextField();
		txtPrix.setBounds(170, 87, 98, 20);
		panel.add(txtPrix);
		txtPrix.setColumns(10);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "s\u00E9l\u00E9ction", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_1.setBounds(232, 42, 328, 86);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		lblLibelle = new JLabel("Libell\u00E9 :");
		lblLibelle.setBounds(55, 57, 105, 14);
		panel_1.add(lblLibelle);
		
		this.lesTarifs = lesTarifs;
		
		lstTarif = new JComboBox <String>();
		for(Tarif t: lesTarifs)
		{
			lstTarif.addItem(t.getLibTarif());
		}
		lstTarif.setBounds(106, 53, 143, 22);
		panel_1.add(lstTarif);
		
		lblId = new JLabel("ID :");
		lblId.setBounds(55, 22, 46, 14);
		panel_1.add(lblId);
		
		txtId = new JTextField();
		txtId.setEditable(false);
		txtId.setBounds(182, 22, 67, 20);
		panel_1.add(txtId);
		txtId.setColumns(10);
		
		btnSupprimer = new JButton("Supprimer");
		btnSupprimer.setBounds(24, 381, 130, 23);
		getContentPane().add(btnSupprimer);
		
		btnModifier = new JButton("Modifier");

		btnModifier.setBounds(608, 381, 130, 23);
		getContentPane().add(btnModifier);


	}

	
	 public void actionPerformed(ActionEvent e) {
			
			
			if (e.getSource() == this.btnModifier)
			{
				Tarif unTarif = (Tarif) lstTarif.getSelectedItem();

	             unTarif.setLibTarif(txtLibelle.getText());
	             unTarif.setPrix(Double.parseDouble(txtPrix.getText()));
	             lesTarifs.setElementAt(unTarif,lstTarif.getSelectedIndex());

				
	             if(!txtLibelle.getText().equals("") && !txtPrix.getText().equals(""))
	             {
	                     if(DaoProjetCinema.modifierTarif(unTarif) == 1)
	                     {
	                    	 	JOptionPane.showMessageDialog(null, "Le tarif a été modifié.");  
	                     }
	                         
	              }   
	             else
	             {
	                     JOptionPane.showMessageDialog(null, "Tout les champs doivent être renseigné.", "Erreur", JOptionPane.ERROR_MESSAGE);
	             }
	                 
			}
			if(e.getSource() == btnSupprimer)
			{
				{
	                Tarif leTarif;
	                leTarif = (Tarif) lstTarif.getSelectedItem();

	                if(DaoProjetCinema.supprimerTarif(leTarif) == 1)
	                {
	                        lesTarifs.remove(leTarif);
	                        JOptionPane.showMessageDialog(null, "Le Film a été supprimé", "Information", JOptionPane.INFORMATION_MESSAGE);
	                        txtLibelle.setText("");
	                        txtPrix.setText("");
	                        txtId.setText("");
	                }
	                else
	                {
	                        JOptionPane.showMessageDialog(null, "Le Film n'a pas pu être supprimé", "Information", JOptionPane.ERROR_MESSAGE);
	                }
				}
			}

		}

	public void itemStateChanged(ItemEvent arg0) {
	}
}
